<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
/*var s = "abcabcdefghigk";
var i = s.indexOf("abc", 1);
console.log(i);
console.log(s.lastIndexOf("abc"));*/

// 现有一个字符串, 统计一个指定的子字符串在原字符串中出现的次数  "309480abcafasdabc" "abc"
function substrCount(str, substr){
    var count = 0;  // 记录子字符串出现的次数
    var len = substr.length;    // 子字符串的长度
    var fromIndex = 0;  // 每次查找开始的位置
    for (; fromIndex < str.length;){
        var index = str.indexOf(substr, fromIndex);
        if (index != -1){
            count++;
            fromIndex = index + len;
        }else{
            break;
        }
    }
    return count;
}
var s1 = "alksdfjafldsjfabcljlabc dslkfafabcjlasdjflsjdls abc dslfjlksdajflkasdjflasjabc ldsajflasjabc"
var s2 = "abc"
console.log(substrCount(s1, s2));

</script>
</body>
</html>